package com.sphd.Restaurant.presenter;

import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.ScoresView;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Hu on 2016/10/11.
 */

public class ScoresP extends BasePresenter implements VolleyCallBack<JSONObject> {
    ScoresView scoresView;

    public ScoresP(ScoresView scoresView) {
        this.scoresView = scoresView;
    }

    @Override
    public void addData(String url) {
        scoresView.showdialog();
        sendJsonObjectUrl(url, this);
    }

    @Override
    public void onResponse(JSONObject jsonObject) {

        try {
            JSONArray array = new JSONArray(jsonObject.getString("res"));
            for (int i = 0; i < array.length(); i++) {
                JSONObject job = array.getJSONObject(i);
                if (job.has("mem_integral")) {
                    scoresView.sendList(job.getString("mem_integral"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        scoresView.dissmissdialog();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        scoresView.dissmissdialog();
        Toast.makeText(context, VolleyErrorHelper.getMessage(error), Toast.LENGTH_SHORT).show();
    }
}
